<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Feedback as feedbackModel;
use think\facade\Db;
use think\facade\View;

class Feedback extends Base{
    // 新增编辑
    public function edit($id=0){
        if(!request()->isPost()){
            $rs = feedbackModel::find($id);
            View::assign('rs',$rs);
            // 用户
            $userRs = Db::name('user')->where('id',$rs['userid'])->find();
            View::assign('userRs',$userRs);

            return view();
        }else{
			try {
                feedbackModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}

    // 列表
    public function index(){
    	$signid = input('signid'); $keyword = input('keyword');
    	$order = ['sequence'=>'desc','id'=>'desc'];

        // 赋值状态
        if(!is_numeric($signid)) $signid = 3;
        View::assign('signid',$signid);
    	
		$rs = feedbackModel::index($signid,0,$this->lang,$keyword,$order);

		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());
		
        return view();
    }

    // 回复
    public function reply($id=0){
        $id = input('id'); $signid = input('signid');
        if(!request()->isPost()){
            $rs = Db::name('feedback')->where('id',$id)->find();
            // 留言信息
            View::assign('rs',$rs);
            // 用户
            $userRs = Db::name('user')->where('id',$rs['userid'])->find();
            View::assign('userRs',$userRs);
            // 回复列表
            $replylist = Db::view('feedback','*')->view('user',['avatar','nickname'],'user.id=feedback.userid')->where('feedback.parentid',$id)->select();
            View::assign('replylist',$replylist);

            return view();
        }else{
            try {
                feedbackModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }       
    }

    // 单个更新状态
    public function update_signid(){
        $id = input('id'); $signid = input('signid');
        // 参数不能为空
        if(empty($id) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('id'=>$id)]);
        }
        // 软删除
        if($signid == 9) $signid = time();

        try {
            Db::name('feedback')->where('id',$id)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    // 批量更新状态
    public function updateall_signid(){
        $selectid = input('selectid/a'); $signid = input('signid');
        // 参数错误
        if(empty($selectid) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }
        // 软删除
        if($signid == 9) $signid = time();

        try {
            Db::name('feedback')->where('id','in',$selectid)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
	
	// 单条删除
    public function delete_one(){
	    $id = input('id');
        // 参数不能为空
        if(empty($id)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('id'=>$id)]);
        }
        
		try {
            // 删除子信息
            Db::name('feedback')->where('parentid',$id)->delete();
            $feedback = feedbackModel::find($id);
            $feedback->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
	
	//批量删除
    public function delete_all(){
		$selectid = input('selectid/a');
        // 参数错误
		if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }
		
		try {
            // 删除子信息
            Db::name('feedback')->where('parentid','in',$selectid)->delete();
            // 执行删除
            feedbackModel::destroy($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }	
}